const Router = require("@koa/router");
const router = new Router();
const {
  modelAdministrator,
  modelDailyrecom,
  provinceSchema,
  modelFourTravel,
} = require("@/models/collection-pc");
const { Serchtravel, Classifytravels } = require("@/config/valiData");
const { Auth } = require("@/token/auth");
const { Authuid } = require("@/token/auth_uid");
const moment = require("moment");
moment.locale("zh-cn");
const {
  modelUser,
  modelArticle,
  modelConcern,
} = require("@/models/collection");
const { looKup, looKupRecommend } = require("@/config/lookup");
//获取前10个热门城市
router.get("/hot-city", async (ctx) => {
  const res = await modelArticle.aggregate([
    {
      $group: {
        _id: "$city",
        count: { $sum: 1 },
        image: { $first: "$cover_image.url" },
      },
    },
    {
      $sort: {
        count: -1,
      },
    },
    {
      $limit: 10,
    },
  ]);
  ctx.send("SUCCESS", 200, res);
});

//获取搜索关键词
router.get("/search-travel", async (ctx) => {
  const { keywords } = ctx.query;
  Serchtravel(keywords);

  if (keywords.trim() === "") {
    ctx.send("SUCCESS", 200, []);
    return;
  }

  const query = { $regex: keywords, $options: "i" };

  // 查询文章
  const articles = await modelArticle
    .find({
      $or: [
        { city: query },
        { address: query },
        { province: query },
        { tag: keywords },
      ],
    })
    .select("city address tag");

  // 查询用户
  const users = await modelUser
    .find({
      $or: [{ nickname: query }, { uid: keywords }],
    })
    .select("nickname");

  // 只提取值
  const results = [
    ...articles
      .map((item) => [item.city, item.address, ...(item.tag || [])])
      .flat(),
    ...users.map((item) => item.nickname),
  ].filter(Boolean); // 过滤掉 null 和 undefined
  // 去重
  const uniqueResults = [...new Set(results)];
  ctx.send("SUCCESS", 200, uniqueResults);
});

//根据关键词搜索文章
router.get("/search-article", async (ctx) => {
  const { keywords, page } = ctx.query;
  Classifytravels(keywords, page);
  const query = { $regex: keywords, $options: "i" };
  const res = await modelArticle.aggregate([
    {
      $match: {
        $or: [
          { title: query },
          { content: query },
          { city: query },
          { tag: { $in: [keywords] } },
          { address: query },
        ],
      },
    },
    { $skip: (page - 1) * 6 },
    { $limit: 6 },
    looKup().model_user,
    looKup().model_like,
    looKup().projectcard,
  ]);
  ctx.send("SUCCESS", 200, res);
});

//根据关键词搜索作者
router.get("/search-author", new Authuid().m, async (ctx) => {
  const { keywords, page } = ctx.query;
  Classifytravels(keywords, page);
  const query = { $regex: keywords, $options: "i" };
  const myUid = ctx.auth.uid ? ctx.auth.uid : "m1";
  const res = await modelUser.aggregate([
    {
      $match: {
        $or: [{ nickname: query }, { uid: keywords }],
      },
    },
    { $skip: (page - 1) * 10 },
    { $limit: 10 },
    {
      $lookup: {
        from: modelConcern.collection.name,
        localField: "uid",
        foreignField: " im_concerned_uid",
        as: "concern_user",
      },
    },
    {
      $project: {
        _id: 1,
        nickname: 1,
        avatarUrl: 1,
        uid: 1,
        numberOfFans: { $size: "$concern_user" },
        concernedUser: {
          $in: [myUid, "$concern_user.user_uid"],
        },
      },
    },
  ]);
  ctx.send("SUCCESS", 200, res);
});

module.exports = router.routes();
